    /****************************************** 
  	 初始化数据
  	*******************************************/
  
  //echart   
  var bbtChart;
  var gridX = 35;
  var gridY = 10;
  var gridX2 = 10;
  var gridY2 = 30;
  	
  //X轴
  var currentDateStr;	
  var xAxisDateArray;
  var xTickLength;
  var xAxisLabelMargin = -20;
  
  //Y轴 
  var yAxisMinTemperature;
  var yAxisMaxTemperature;
	
  var bbtTitle = '基础体温';
  var bgColor = '背景颜色';
  var yjqTitle;
  var lpqTitle;
  var plqTitle;
  var htqTitle;
  var aqqTitle;
  var wxqTitle;
	
  var bbtDataArray = new Array();
  var polishedBBTDataArray = new Array();
  var bgColorDataArray = new Array();
  var yAxisInterval;
  var yAxisSplitNumber;

  //背景色
  var yjqBackgroundColor;
  var state = localStorage.getItem("state");
  if(state == 0)
  	yjqBackgroundColor = 'rgba(251,226,226,0.5)';
  else
  	yjqBackgroundColor = 'rgba(0,0,0,0)'; //避孕，不显示颜色

  //var lpqBackgroundColor = 'rgba(192,231,255,0.5)';
  var lpqBackgroundColor = 'rgba(0,0,0,0)';
  var plqBackgroundColor = 'rgba(228,246,219,0.5)';
  //var htqBackgroundColor = 'rgba(250,228,169,0.5)';
  var htqBackgroundColor = 'rgba(0,0,0,0)';
  var aqqBackgroundColor = plqBackgroundColor;
  var wxqBackgroundColor = 'rgba(211,60,110,0.2)';
  var unknownBackgroundColor = 'rgba(255,255,255,0.5)';
 
    function initBBTChartData(){
    	initOrientation();	
    	initXTickLength();
    	initXAxisData();
    	
    	initBBTChartZone();        	
    	initShowTip();	

					
		initYAxisData();
		//initYAxisDiv();
		
		initBgColorData();
    }
    
    function initOrientation(){	  	
	  	if(isPortrait())	  		
	  		setPortraitData(); 		
	  	else		
  			setLandscapeData();   			  		
    }
    
    function initBBTChartZone(){
		setBBTChartFixZone(bbtChartWidth, bbtChartHeight);
		setBBTChartScrollZone(bbtChartWidth, bbtChartHeight);
		setMonthZone(bbtChartWidth, bbtChartHeight);
    	setBBTChartZone(bbtChartWidth, bbtChartHeight);    
    }
    
    function getScreenWidth(){
    	return window.innerWidth;
    }
    
    function getScreenHeight(){
    	//if(isIphone())
    	//	return window.innerHeight + 49;
    	//else
    		return window.innerHeight;
    }
    
    //作为iphone的横屏时,高度返回：补偿下部控件栏高度为49的workaround
    function isIphone(){
    	var u = navigator.userAgent;
    	if(u.indexOf('iPhone') > -1)
    		return true;
    	else
    		return false;
    }
    
    function setBBTChartFixZone(bbtChartWidth, bbtChartHeight){
    	document.getElementById('div_fix_zone').style.width = bbtChartWidth + "px";
    	document.getElementById('div_fix_zone').style.height = bbtChartHeight + "px";
    }
    
    function setBBTChartScrollZone(bbtChartWidth, bbtChartHeight){
    	document.getElementById('div_scroll_zone').style.width = bbtChartWidth + "px";
    	document.getElementById('div_scroll_zone').style.height = bbtChartHeight + "px";    	
    }       
    
    function setMonthZone(bbtChartWidth, bbtChartHeight){
    	document.getElementById('div_month').style.width = bbtChartWidth + "px";
    	document.getElementById('div_month').style.height = bbtChartHeight + "px"; 
    	document.getElementById('div_month').style.lineHeight = bbtChartHeight + "px"; 
    	document.getElementById('div_month').innerHTML = getBgMonthTip(getCurrentDateStr());    	   	
    }
    
    function setBBTChartZone(bbtChartWidth, bbtChartHeight){      	
    	document.getElementById('bbtLineChart').style.width = xTickLength * showDaysTotalNum + "px";
    	document.getElementById('bbtLineChart').style.height = bbtChartHeight + "px";  
    	document.getElementById('div_scroll_zone').scrollLeft = getMarginLeft();       	 		
    }      
    
     /****************************************** 
  	 设置画完图后让今天数据处于图中的滑动距离
  	*******************************************/
    function getMarginLeft(){
    	return getDeltaMoveDays() * xTickLength;
    }
    
    //为了让今天的温度居中，需要位移Delta个日期：真正今天之前的
    function getDeltaMoveDays(){
    	var semiSreenDaysNum = (SCALE_FACTOR - 1) / 2;
    	var beforeDaysNum = getBeforeShowDaysNum();
    	var afterDaysNum = showDaysTotalNum - beforeDaysNum - 1;

    	if((beforeDaysNum > semiSreenDaysNum) && (afterDaysNum >= semiSreenDaysNum))
    		return beforeDaysNum - semiSreenDaysNum;
    	else if((beforeDaysNum > semiSreenDaysNum) && (afterDaysNum < semiSreenDaysNum))
    		return showDaysTotalNum - SCALE_FACTOR;
    	else
    		return 0;
    }
    
    function getBeforeShowDaysNum(){
    	var currentDateStr = getCurrentDateStr();
    	
    	for(var i = 0; i < showDaysTotalNum; i++){
    		if(xAxisDateArray[i] == currentDateStr)
    			break;   			
    	}
    	
    	return i; 	 
    }
    
    function initShowTip(){
    	if(portraitOrLandscape == "portrait")
  			showTip = false;
  		else
  			showTip = true;
  	}
  
    //x轴数据初始化
    function initXAxisData(){   	
		initXAxisDateArray();			
		getFirstLastXAxisLabelDate(xAxisDateArray);			
    }
    
    function initXAxisDateArray(){
        // 这是所有的customer数据
    	// var customerDateStr = localStorage.getItem("customerDateKey");
		// xAxisDateArray = getCustomerDateArray(customerDateStr);

		// 这是截取三个月经周期的日期数据
		var limitedCustomerDateArrayStr = localStorage.getItem("limitedCustomerDateArrayKey");
		xAxisDateArray = limitedCustomerDateArrayStr.split(",");
    }

    function getFirstLastXAxisLabelDate(xAxisDateArray){    	
		preSwipeFirstXAxisDate = xAxisDateArray[0];	 	
		preSwipeLastXAxisDate = xAxisDateArray[xAxisDateArray.length - 1];
    }
    
    function initXTickLength(){
	  	var xTickTotal = bbtChartWidth - gridX - gridX2;
	  	xTickLength = xTickTotal / SCALE_FACTOR;
   }
    
    //y轴数据初始化
    function initYAxisData(){    			
	  	setYAxisTemperatureScope();
	  	setPeriodTitle();
	    setYAxisSplit();
	    setYAxisData();
    }
    
    function setYAxisTemperatureScope(){
    	yAxisMinTemperature = getMinTemperature();
	  	yAxisMaxTemperature = getMaxTemperature();
    }
    
    function setPeriodTitle(){
       	var lang = localStorage.getItem("language");

        if(lang == "chinese"){
            yjqTitle = '月经期';
            lpqTitle = '卵泡期';
            plqTitle = '排卵期';
            htqTitle = '黄体期';
            aqqTitle = '安全期';
            wxqTitle = '危险期';
            unknownTitle = '暂未知';
	    }else if(lang == "english"){
	        yjqTitle = 'Menstruation';
            lpqTitle = 'Follicular Phase';
            plqTitle = 'Fertile Window';
            htqTitle = 'Luteal Phase';
            aqqTitle = 'Safe Period';
            wxqTitle = 'Risk Period';
            unknownTitle = 'Unknown';
	    }
	}
    
    function setYAxisSplit(){
    	if(portraitOrLandscape == "portrait"){
    		var lang = localStorage.getItem("language");

			if(lang == "chinese")
				yAxisInterval = 5;
			else if(lang == "english")
				yAxisInterval = 4;
    	}

    	if(portraitOrLandscape == "landscape")
    		yAxisInterval = 10;
    		
	    yAxisSplitNumber = yAxisInterval * (yAxisMaxTemperature - yAxisMinTemperature);
    }
    
    function setYAxisData(){
    	for(var i = 0; i < xAxisDateArray.length; i++){
    		bbtDataArray[i] = customerDateTemperatureMap.get(xAxisDateArray[i]);
    		var polishedTemperature = customerDatePolishedTemperatureMap.get(xAxisDateArray[i]);

    		if(bbtDataArray[i] != "-"){
    			var heart = customerDateHeartMap.get(xAxisDateArray[i]);

    			if(heart == "yes")
    				polishedBBTDataArray.push({ value: getDisplayTempValue(bbtDataArray[i]), symbol: 'image://img/bbt/heart.png', symbolSize: 5});
    			else
    				polishedBBTDataArray.push({value: getDisplayTempValue(bbtDataArray[i]), symbol: 'circle', symbolSize: 3});
    		}
    		else if(bbtDataArray[i] == "-" && polishedTemperature != "-")
    			polishedBBTDataArray.push({ value: getDisplayTempValue(polishedTemperature), symbol: 'none'});
            else
            	polishedBBTDataArray.push("-");
    	}
    }

    function getPreviousValidTemperature(index){
      	if(index == 0)
      		return 0;

      	var validData = "-";
      	for(var i = index - 1; i >= 0; i--){
      		if(bbtDataArray[i] != "-"){
      			validData = bbtDataArray[i];
      			break;
      		}
      	}

      	if(i == -1 && validData == "-")
      		return 0;

      	return validData;
    }

    function getFirstValidTemperatureDate(){
    	var dateStr = "-";

    	for(var i = 0; i < xAxisDateArray.length; i++){
    		if(customerDateTemperatureMap.get(xAxisDateArray[i]) != "-"){
    			dateStr = xAxisDateArray[i];
    			break;
    		}
    	}

    	return dateStr;
    }

    //目前只支持2等份/5等份的Y轴悬浮刻度
    function initYAxisDiv(){    	
    	var yAxisLength = bbtChartHeight - gridY - gridY2;
    	var yAxisTickLength = yAxisLength / yAxisSplitNumber;
	    	    	      	
    	document.getElementById('bbtLineChartYAxis').style.marginTop = gridY + "px";
    	document.getElementById('bbtLineChartYAxis').style.marginBottom = gridY2 + "px";
    	document.getElementById('bbtLineChartYAxis').style.height = yAxisLength + "px";
    	document.getElementById('bbtLineChartYAxisTable').style.height = yAxisTickLength * (yAxisSplitNumber + 1) + "px";
    	document.getElementById('bbtLineChartYAxisTable').innerHTML = getYAxisTableContent(yAxisTickLength); 
    	document.getElementById('bbtLineChartYAxisTable').style.marginTop = - yAxisTickLength / 2 + "px";
    }     

    function getYAxisTableContent(yAxisTickLength){
    	var unit = localStorage.getItem("unit");

		if(unit == "centigrade")
			return getYAxisTableContentC(yAxisTickLength);
		else if(unit == "fahrenheit")
			return getYAxisTableContentF(yAxisTickLength);
    }


    function getYAxisTableContentC(yAxisTickLength){
    	var tempYAxisTickValue = 1/yAxisInterval;
    	var yAxisTickValue = tempYAxisTickValue.toFixed(1);
    	
    	var yAxisTableContent = "";
    	var preRowStr = '<tr> <td style="height:'+yAxisTickLength+';">';
    	var postRowStr = '</td> </tr>';

    	for(var i = yAxisMaxTemperature.toFixed(1); ; ){    	
    		if(parseInt(i) == i)
    			yAxisTableContent += preRowStr + i + postRowStr;
//    		else if(i*10 > 360 && i*10 < 370){
//    			if((i*10)%2 == 0)
//    				yAxisTableContent += preRowStr + "." + (i*10)%10 + postRowStr;
//    			else
//    				yAxisTableContent += preRowStr + "" + postRowStr;
//    		}
    		else
    			yAxisTableContent += preRowStr + "." + (i*10)%10 + postRowStr;  
    			 			
    		i = i - yAxisTickValue;	
    		i = i.toFixed(1);   
    		
    		if(i*10 == yAxisMinTemperature*10) 		
    		{
    			yAxisTableContent += preRowStr + "." + (i*10)%10 + postRowStr;
    			break;
    		}  		
    	}
    	
    	return yAxisTableContent;
    }

    function getYAxisTableContentF(yAxisTickLength){
        	var tempYAxisTickValue = 1/yAxisInterval;
        	var yAxisTickValue = tempYAxisTickValue.toFixed(2);

        	var yAxisTableContent = "";
        	var preRowStr = '<tr> <td style="height:'+yAxisTickLength+';">';
        	var postRowStr = '</td> </tr>';

        	for(var i = yAxisMaxTemperature.toFixed(2); ; ){
        		if(parseInt(i) == i)
        			yAxisTableContent += preRowStr + i + postRowStr;
        		else
        			yAxisTableContent += preRowStr + "." + (i*100)%100 + postRowStr;

        		i = i - yAxisTickValue;
        		i = i.toFixed(2);

        		if(i*100 == yAxisMinTemperature*100)
        		{
        			yAxisTableContent += preRowStr + i + postRowStr;
        			break;
        		}
        	}

        	return yAxisTableContent;
        }
    
    
    function initBgColorData(){
    	for(var i = 0; i < xAxisDateArray.length; i++)
    		bgColorDataArray[i] = getPeriodBackGroundColor(xAxisDateArray[i]);
    }
    
